package com.day1;

import com.VO.ResponceVO;
import com.alibaba.fastjson.JSON;
import hk.JdbcUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/addUser")
public class AddUser extends HttpServlet{
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //super.doGet(req, resp);
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        System.out.println("转页成功======");

        //获取参数
        int id = Integer.parseInt(req.getParameter("id"));
        String username = req.getParameter("username");
        int passward = Integer.parseInt(req.getParameter("passward"));
        int age = Integer.parseInt(req.getParameter("age"));
        String address = req.getParameter("address");
        System.out.println("========"+id+""+username+""+passward+""+age+""+address);
        ResponceVO responceVO = new ResponceVO();

        //操作数据库
        try {
            int result = JdbcUtil.addUser(id,username,passward,age,address);
            responceVO.setResult(result);
            System.out.println("+++++++++++"+responceVO);
            resp.getWriter().print(JSON.toJSONString(responceVO));
        } catch (Exception e) {

            responceVO.setResult(0);
            resp.getWriter().print(JSON.toJSONString(responceVO));
        }


    }
    }

